<template>
  <div id="app">
    <router-view :key="key" />
    <main-footer></main-footer>
    <quick-tab></quick-tab>
  </div>
</template>
<script>
import MainFooter from "./components/section/MainFooter.vue";
import QuickTab from "./components/section/QuickTab.vue";
import { getBrands, getDicts, getOpts } from "@/front/assets/api/view.js";
import { autoLogin } from "@/front/assets/api/user.js";
import { Cookie } from "../public/js/utility.js";
export default {
  name: "app",
  components: { MainFooter, QuickTab },
  data() {
    return {};
  },
  computed: {
    key() {
      return this.$route + new Date(); //避免vue复用相同组件，导致子路由页面不刷新的问题
    },
  },
  methods: {
    async initData() {
      let opts = await getOpts();
      let dicts = await getDicts();
      let brands = await getBrands();
      this.$store.commit("save", { path: "opts", data: opts });
      this.$store.commit("save", { path: "dicts", data: dicts });
      this.$store.commit("save", { path: "brands", data: brands });
      console.log("opts", this.$store.state.opts);
      console.log("dicts", this.$store.state.dicts);
      console.log("brands", this.$store.state.brands);
      this.backupsUserInfo();
      this.autoLogin();
    },
    backupsUserInfo() {
      localStorage.setItem(
        "userInfo",
        JSON.stringify(this.$store.state.userInfo)
      );
      console.log(JSON.parse(localStorage.getItem("userInfo")));
    },
    async autoLogin() {
      let tel = new Cookie().get("tel");
      if (tel) {
        let { code, msg, data } = await autoLogin({ type: "auto", tel });
        if (code == 1) {
          this.$store.commit("save", { path: "hasLogin", data: true });
          this.$store.commit("save", { path: "userInfo", data });
        } else {
          this.$message.error(msg);
        }
      }
    },
  },
  created() {
    this.initData();
  },
  mounted() {},
};
</script>

<style lang="scss"></style>
